package Sort;

import java.util.Arrays;
import java.util.Collections;

public class _324_WiggleSortII {
    public void wiggleSort(int[] nums) {
        if (nums.length == 0 || nums.length == 1) {
            return;
        }
        int len = nums.length;
        int[] res = new int[nums.length];
        Arrays.sort(nums);
        int middle = len / 2;
        int pos = 0;
        int add = 0;
        if (len % 2 != 0) {
            add = 1;
            res[len - 1] = nums[middle];
        }
        for (int i = 0; i < middle; i++) {
            res[2 * i] = nums[middle - pos - add];
            res[2 * i + 1] = nums[len - pos - 1];
            pos++;
        }
        //copy the new array to the old array
        System.arraycopy(res, 0, nums, 0, len);
    }
}
